package com.slz3.business.module.goodsManagement.gooOutWarehouse.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooOutWarehouse.domain.GooOutWarehouse;
import com.slz3.business.module.goodsManagement.gooOutWarehouse.domain.GooOutWarehouseFactory;
import com.slz3.business.module.goodsManagement.gooOutWarehouse.steward.GooOutWarehouseSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class GooOutWarehouseServiceImpl implements GooOutWarehouseService {
    @Override
    public GooOutWarehouse save(GooOutWarehouse gooOutWarehouse) {
        return Repositorys.gooOutWarehouse.save(GooOutWarehouseFactory.initBaseFields(gooOutWarehouse));
    }

    @Override
    public Page<GooOutWarehouse> page(MultiValueMap<String, String> params) {
        Page<GooOutWarehouse> gooOutWarehousePage = Repositorys.gooOutWarehouse.findAll(
                GooOutWarehouseSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooOutWarehousePage;
    }


    @Override
    public GooOutWarehouse modify(GooOutWarehouse gooOutWarehouseNew) {
        return Repositorys.gooOutWarehouse.saveAndFlush(GooOutWarehouseFactory.initBaseFields(gooOutWarehouseNew));
    }

    @Override
    public Optional<GooOutWarehouse> single(UUID uuid) {
        return Repositorys.gooOutWarehouse.findById(uuid);
    }

    @Override
    public List<GooOutWarehouse> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooOutWarehouse.findAllById(uuids);
    }

    @Override
    public List<GooOutWarehouse> delete(List<GooOutWarehouse> gooOutWarehouseList) {
        gooOutWarehouseList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooOutWarehouseFactory.initBaseFields(item);
        });
        return Services.gooOutWarehouseTransactional.batchModify(gooOutWarehouseList);
    }

}
